package de.hfu.android;

import de.hfu.helper.DataCarrier;
import de.hfu.helper.DataParser;
import de.hfu.helper.Helper;
import de.hfu.model.Dish;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.widget.RatingBar;
import android.widget.TextView;

public class GetCommentTask extends AsyncTask<Void, Void, Void> implements OnCancelListener {
	Activity context;
	ProgressDialog dialog;
	String result;
	float rateResult;
	
	public GetCommentTask(Activity context) {
		this.context = context;
		result = "";
		rateResult = 0;
	}
	
	@Override
	protected void onPreExecute() {
		//dialog = ProgressDialog.show(context, "", context.getString(R.string.loading), true, true);
		//dialog.setOnCancelListener(this);
	}
	
	@Override
	protected void onPostExecute(Void res) {
		//dialog.dismiss();
		TextView lblComment = (TextView) context.findViewById(R.id.lblComments);
		lblComment.setText(result);
		
		RatingBar overallRate = (RatingBar) context.findViewById(R.id.overall_ratebar);
		overallRate.setRating(rateResult);
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		DataParser parser = DataParser.getInstance();
		Dish dish = DataCarrier.getInstance().getDish();
		result = parser.getCommentFromGAE(dish.getTitle(), dish.getDate());
		
		float overall = parser.getRatingFromGAE(dish.getTitle(), dish.getDate());		
		
		if (!Helper.isEqual(overall, -1)) {
			rateResult = overall;
		} else
			rateResult = -1;
		return null;
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		this.cancel(true);
	}

}
